package com.learn.core.controller.manager;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.learn.common.annotation.Log;
import com.learn.common.annotation.UserLoginToken;
import com.learn.common.enums.BusinessType;
import com.learn.common.enums.OperatorType;
import com.learn.common.enums.ResultEnum;
import com.learn.common.utils.JwtUtil;
import com.learn.common.utils.PageResult;
import com.learn.common.utils.ResultUtil;
import com.learn.common.utils.StringUtils;
import com.learn.common.utils.result.CommonResult;
import com.learn.core.domain.*;
import com.learn.core.domain.view.UserDetailsGather;
import com.learn.core.dto.LCompetitionNewDTO;
import com.learn.core.dto.LRotatePicturesDTO;
import com.learn.core.service.*;
import com.learn.framework.web.controller.WebController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * Description
 *
 * @author zhaohongyu
 * @ClassName LCompetitionNewController
 * @date 2021.07.07 16:08
 * @describe 赛事新闻报导
 */
@Api("赛事新闻报导")
@RestController
@RequestMapping("/manager/cnew")
public class LCompetitionNewController extends WebController<LCompetitionNew> {

    @Autowired
    private ILCompetitionNewService ilCompetitionNewService;
    @Autowired
    private IUserDetailsGatherService iUserDetailsGatherService;
    @Autowired
    private ILOrganizationService ilOrganizationService;
    @Autowired
    private ILCompetitionUserService ilCompetitionUserService;
    @Autowired
    private ILCompetitionService ilCompetitionService;

    //列表
    @ApiOperation(value = "获取列表")
    @UserLoginToken
    @PostMapping("/list")
    public PageResult<LCompetitionNew> list(@RequestBody LCompetitionNewDTO lCompetitionNewDTO){
        try {
            if (lCompetitionNewDTO==null||lCompetitionNewDTO.getPage()==null||lCompetitionNewDTO.getRows()==null){
                return new PageResult<>(null, null, ResultEnum.ERROR);
            }
            PageHelper.startPage(lCompetitionNewDTO.getPage(),lCompetitionNewDTO.getRows());
            QueryWrapper<LCompetitionNew> queryWrapper = new QueryWrapper<>();
            if(StringUtils.isNotEmpty(lCompetitionNewDTO.getStartTime()))
                queryWrapper.gt("create_time",lCompetitionNewDTO.getStartTime());
            if(StringUtils.isNotEmpty(lCompetitionNewDTO.getEndTime()))
                queryWrapper.le("create_time",lCompetitionNewDTO.getEndTime());
            queryWrapper.orderByAsc("competition_seq");
            Page<LCompetitionNew> lCompetitionNews = (Page<LCompetitionNew>)ilCompetitionNewService.list(queryWrapper);
            if (CollectionUtils.isEmpty(lCompetitionNews)) {
                return new PageResult<>(null, null, ResultEnum.NOT_EEIST);
            }
            for (LCompetitionNew lCompetitionNew: lCompetitionNews) {
                if(null != lCompetitionNew.getCompetitionUser()){
                    QueryWrapper<UserDetailsGather> userDetailsGatherQueryWrapper = new QueryWrapper<>();
                    userDetailsGatherQueryWrapper.eq("user_id",lCompetitionNew.getCompetitionUser());
                    UserDetailsGather userDetailsGather = iUserDetailsGatherService.getOne(userDetailsGatherQueryWrapper);
                    if(null != userDetailsGather){
                        lCompetitionNew.setUserName(userDetailsGather.getUserName());
                        LOrganization lOrganization = ilOrganizationService.getById(userDetailsGather.getOrganizationId());
                        if(null != lOrganization)
                            lCompetitionNew.setOrganizationName(lOrganization.getOrganizationName());
                    }
                }
                if(null != lCompetitionNew.getCompetitionId()){
                    QueryWrapper<LCompetitionUser> lCompetitionUserQueryWrapper = new QueryWrapper<>();
                    lCompetitionUserQueryWrapper.eq("competition_id",lCompetitionNew.getCompetitionId());
                    LCompetitionUser lCompetitionUser = ilCompetitionUserService.getOne(lCompetitionUserQueryWrapper);
                    if(null != lCompetitionUser){
                        lCompetitionNew.setUserScore(lCompetitionUser.getUserScore());
                        lCompetitionNew.setOperationScore(lCompetitionUser.getOperationScore());
                    }
                    LCompetition lCompetition = ilCompetitionService.getById(lCompetitionNew.getCompetitionId());
                    if(null != lCompetition){
                        lCompetitionNew.setCompetitionName(lCompetition.getCompetitionName());
                    }
                }
            }
            // 返回
            return new PageResult(lCompetitionNews.getTotal(), lCompetitionNews, ResultEnum.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new PageResult(null,null,ResultEnum.ERROR);
        }
    }

    //更新
    @ApiOperation(value = "更新")
    @PostMapping("/edit")
    @UserLoginToken
    @Log(title = "更新新闻", businessType = BusinessType.UPDATE, operatorType = OperatorType.SYSTEM)
    public CommonResult<Object> edit(@RequestBody LCompetitionNew lCompetitionNew, HttpServletRequest request){
        // token
        String token = request.getHeader("token");
        String userId = JwtUtil.getUserId(token);
        if(lCompetitionNew!=null){
            lCompetitionNew.setUpdateTime(new Date());
            lCompetitionNew.setUpdateUser(userId);
            if(ilCompetitionNewService.updateById(lCompetitionNew)){
                return (CommonResult) ResultUtil.success(ResultEnum.SUCCESS);
            }
        }
        return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
    }

    //新增
    @ApiOperation(value = "添加")
    @PostMapping("/add")
    @UserLoginToken
    @Log(title = "添加新闻", businessType = BusinessType.INSERT, operatorType = OperatorType.SYSTEM)
    public CommonResult<Object> add(@RequestBody LCompetitionNew lCompetitionNew, HttpServletRequest request){
        // token
        String token = request.getHeader("token");
        String userId = JwtUtil.getUserId(token);
        try {
            if(lCompetitionNew!=null) {
                lCompetitionNew.setCreateTime(new Date());
                lCompetitionNew.setCreateUser(userId);
                if (ilCompetitionNewService.save(lCompetitionNew)) {
                    return (CommonResult) ResultUtil.success(ResultEnum.SUCCESS);
                }
            }
        }catch (Exception e){
            return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
        }
        return  (CommonResult)ResultUtil.error(ResultEnum.ERROR);
    }


    //删除
    @ApiOperation(value = "删除")
    @PostMapping("/del")
    @UserLoginToken
    @Log(title = "删除新闻", businessType = BusinessType.DELETE, operatorType = OperatorType.SYSTEM)
    public CommonResult<Object> del(@RequestBody LCompetitionNew lCompetitionNew){
        try {
            if(lCompetitionNew!=null){
                if(lCompetitionNew.getCompetitionNewId()!=null){
                    QueryWrapper<LCompetitionNew> queryWrapper = new QueryWrapper<LCompetitionNew>();
                    queryWrapper.in(true, "competition_new_id", lCompetitionNew.getCompetitionNewId());
                    if(ilCompetitionNewService.remove(queryWrapper)){
                        return (CommonResult)ResultUtil.success(ResultEnum.SUCCESS);
                    }
                }
            }
        }catch (Exception e)
        {
            return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
        }
        return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
    }

//    //上架/下架
//    @ApiOperation(value = "上架/下架")
//    @PostMapping("/editState")
//    @UserLoginToken
//    @Log(title = "上架/下架", businessType = BusinessType.DELETE, operatorType = OperatorType.SYSTEM)
//    public CommonResult<Object> editState(@RequestBody LCompetitionNew lCompetitionNew){
//        try {
//            if(lCompetitionNew!=null){
//                if(lCompetitionNew.getCompetitionNewId()!=null){
//                    QueryWrapper<LCompetitionNew> queryWrapper = new QueryWrapper<LCompetitionNew>();
//                    queryWrapper.in(true, "competition_new_id", lCompetitionNew.getCompetitionNewId());
//                    if(ilCompetitionNewService.remove(queryWrapper)){
//                        return (CommonResult)ResultUtil.success(ResultEnum.SUCCESS);
//                    }
//                }
//            }
//        }catch (Exception e)
//        {
//            return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
//        }
//        return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
//    }
}
